package com.huiyujiaoyu.util;

import android.content.Context;
import android.graphics.Paint;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

public class MyClickableSpan extends ClickableSpan {
    private Object map;
    MyClickable myClickable;

    public MyClickableSpan(Object map, MyClickable myClickable) {
        this.map = map;
        this.myClickable = myClickable;
    }

    @Override
    public void onClick(View arg0) {
        myClickable.myClickable(map);
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        // 设置文字颜色
        // ds.setColor(getResources().getColor(R.color.title_bg));
        // 设置下划线.
        ds.setUnderlineText(false);
    }
    public static void setClickableSpanClick(SpannableString ss, int start, int end,
                                        Object mapp, MyClickable myClickable) {
        // 点击事件
        ss.setSpan(new MyClickableSpan(mapp, myClickable), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    public static void setClickableSpanColor(SpannableString ss, int start, int end, int color) {
        //设置颜色
        ss.setSpan(new ForegroundColorSpan(color), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    public static void setClickableSpanSize(SpannableString ss, int start, int end,
                                       int size) {
        // 设置大小
        ss.setSpan(new AbsoluteSizeSpan(size), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    public static void setClickableSpan(SpannableString ss, int start, int end,
                                        Object mapp, int color, MyClickable myClickable) {
        // 点击事件
        ss.setSpan(new MyClickableSpan(mapp, myClickable), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置颜色
        ss.setSpan(new ForegroundColorSpan(color), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    public static void setClickableSpan(SpannableString ss, int start, int end,
                                        Object mapp, int color, int size, MyClickable myClickable) {
        // 点击事件
        ss.setSpan(new MyClickableSpan(mapp, myClickable), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置颜色
        ss.setSpan(new ForegroundColorSpan(color), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置大小
        ss.setSpan(new AbsoluteSizeSpan(size), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    public static void setSpan(SpannableString ss, int start, int end,
                               boolean setSize, int size, boolean setColor, int color) {
        // 设置颜色
        if (setColor)
            ss.setSpan(new ForegroundColorSpan(color), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (setSize)
            ss.setSpan(new AbsoluteSizeSpan(size), start, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    // 设置才能点击
    public static void setClickeAndBgColor(Context c, TextView tv) {
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setHighlightColor(c.getResources().getColor(
                android.R.color.transparent));
    }

    public interface MyClickable {
        void myClickable(Object map);
    }

    public static void setText(TextView tv, String rsd) {
        tv.setText(Html.fromHtml(rsd));
    }

    public static void setTextLine(TextView tv, int type) {
        switch (type) {
            case 0:
                tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
                break;
            case 1:
                tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
                break;
        }

        tv.getPaint().setAntiAlias(true);//抗锯齿
    }


}
